package com.hybench.stats;
/**
 *
 * @time 2023-03-04
 * @version 1.0.0
 * @file Histogram.java
 * @description
 *   calc histogram during workload running
 **/

import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics;
import java.util.concurrent.atomic.AtomicLong;

public class Histogram {
    public SynchronizedDescriptiveStatistics[] apstat = new SynchronizedDescriptiveStatistics[13];
    public SynchronizedDescriptiveStatistics[] xpstat_iq = new SynchronizedDescriptiveStatistics[6];
    public SynchronizedDescriptiveStatistics[] xpstat_at = new SynchronizedDescriptiveStatistics[6];
    public SynchronizedDescriptiveStatistics[] tpstat = new SynchronizedDescriptiveStatistics[18];
    // 成功计数器
    public AtomicLong[] apSuccessCount = new AtomicLong[13];
    public AtomicLong[] xpIqSuccessCount = new AtomicLong[6];
    public AtomicLong[] xpAtSuccessCount = new AtomicLong[6];
    public AtomicLong[] tpSuccessCount = new AtomicLong[18];
    // 失败计数器
    public AtomicLong[] apFailureCount = new AtomicLong[13];
    public AtomicLong[] xpIqFailureCount = new AtomicLong[6];
    public AtomicLong[] xpAtFailureCount = new AtomicLong[6];
    public AtomicLong[] tpFailureCount = new AtomicLong[18];

    public Histogram(){
        for(int i = 0;i<13;i++){
            apstat[i] = new SynchronizedDescriptiveStatistics();
            apSuccessCount[i] = new AtomicLong(0);
            apFailureCount[i] = new AtomicLong(0);
        }
        for(int i = 0;i<18;i++){
            tpstat[i] = new SynchronizedDescriptiveStatistics();
            tpSuccessCount[i] = new AtomicLong(0);
            tpFailureCount[i] = new AtomicLong(0);
        }
        for(int i = 0;i<6;i++){
            xpstat_iq[i] = new SynchronizedDescriptiveStatistics();
            xpIqSuccessCount[i] = new AtomicLong(0);
            xpIqFailureCount[i] = new AtomicLong(0);
        }
        for(int i = 0;i<6;i++){
            xpstat_at[i] = new SynchronizedDescriptiveStatistics();
            xpAtSuccessCount[i] = new AtomicLong(0);
            xpAtFailureCount[i] = new AtomicLong(0);

        }
    }
    public SynchronizedDescriptiveStatistics getAPItem(int index){
        return apstat[index];
    }

    public SynchronizedDescriptiveStatistics getTPItem(int index){
        return tpstat[index];
    }

    public SynchronizedDescriptiveStatistics getXPATItem(int index){
        return xpstat_at[index];
    }

    public SynchronizedDescriptiveStatistics getXPIQItem(int index){
        return xpstat_iq[index];
    }

    // 获取AP事务成功次数
    public long getAPSuccessCount(int index) {
        return apSuccessCount[index].get();
    }

    // 获取AP事务失败次数
    public long getAPFailureCount(int index) {
        return apFailureCount[index].get();
    }

    // 获取TP事务成功次数
    public long getTPSuccessCount(int index) {
        return tpSuccessCount[index].get();
    }

    // 获取TP事务失败次数
    public long getTPFailureCount(int index) {
        return tpFailureCount[index].get();
    }

    // 获取XP-IQ事务成功次数
    public long getXPIQSuccessCount(int index) {
        return xpIqSuccessCount[index].get();
    }

    // 获取XP-IQ事务失败次数
    public long getXPIQFailureCount(int index) {
        return xpIqFailureCount[index].get();
    }

    // 获取XP-AT事务成功次数
    public long getXPATSuccessCount(int index) {
        return xpAtSuccessCount[index].get();
    }

    // 获取XP-AT事务失败次数
    public long getXPATFailureCount(int index) {
        return xpAtFailureCount[index].get();
    }

    // 增加AP事务成功计数
    public void incrementAPSuccess(int index) {
        apSuccessCount[index].incrementAndGet();
    }

    // 增加AP事务失败计数
    public void incrementAPFailure(int index) {
        apFailureCount[index].incrementAndGet();
    }

    // 增加TP事务成功计数
    public void incrementTPSuccess(int index) {
        tpSuccessCount[index].incrementAndGet();
    }

    // 增加TP事务失败计数
    public void incrementTPFailure(int index) {
        tpFailureCount[index].incrementAndGet();
    }

    // 增加XP-IQ事务成功计数
    public void incrementXPIQSuccess(int index) {
        xpIqSuccessCount[index].incrementAndGet();
    }

    // 增加XP-IQ事务失败计数
    public void incrementXPIQFailure(int index) {
        xpIqFailureCount[index].incrementAndGet();
    }

    // 增加XP-AT事务成功计数
    public void incrementXPATSuccess(int index) {
        xpAtSuccessCount[index].incrementAndGet();
    }

    // 增加XP-AT事务失败计数
    public void incrementXPATFailure(int index) {
        xpAtFailureCount[index].incrementAndGet();
    }
}
